﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
    public class NewsDAL
    {
        static string conStr = ConfigurationManager.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationManager.AppSettings["dbPath"] + ";";
        public NewsDAL()
        {

        }
        public static bool Add(NewsInfo pi)
        {
            string sql = "insert into news(name,img,click,context,createdate) values('" + pi.name + "','" + pi.img + "'," + pi.click + ",'" + pi.context + "',now())";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Update(NewsInfo pi)
        {
            string sql = "update news set name='" + pi.name + "',img='" + pi.img + "',context='" + pi.context + "',click=" + pi.click + " where id=" + pi.id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static NewsInfo FindOneById(int id)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from news where id=" + id;
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            NewsInfo pi = new NewsInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static List<NewsInfo> FindAll()
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from news where id>6";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            List<NewsInfo> list = new List<NewsInfo>();
            while (dr.Read())
            {
                NewsInfo pi = new NewsInfo();
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
                list.Add(pi);
            }
            dr.Close();
            con.Close();
            return list;
        }
        public static List<NewsInfo> FindTop(int top)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select top " + top + " * from news where id>6 order by id desc";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            List<NewsInfo> list = new List<NewsInfo>();
            while (dr.Read())
            {
                NewsInfo pi = new NewsInfo();
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
                list.Add(pi);
            }
            dr.Close();
            con.Close();
            return list;
        }
        public static NewsInfo FindPOneById(int id)
        {

            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select top 1 * from news where  id > 6 and id <" + id + " order by id desc";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            NewsInfo pi = new NewsInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static NewsInfo FindNOneById(int id)
        {

            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select top 1 * from news where id > 6 and id >" + id + " order by id asc";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            NewsInfo pi = new NewsInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static bool Delete(string ids)
        {
            string sql = "delete from news where id in (" + ids + ")";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Click(int id)
        {
            string sql = "update news set click=click+1 where id=" + id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
